﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using $YourCompany$.$safeprojectname$_Xap.Utilities;

namespace $YourCompany$.$safeprojectname$_Xap
{
    public partial class Page : UserControl
    {
        [ScriptableMember]
        public string CallbackID { get; set; }  //allow property to be set from javascript

        [ScriptableMember]
        public string UserName { get; set; }  //allow property to be set from javascript

        [ScriptableMember]
        public event EventHandler RealNameObtained; //allow javascript to attach to this event

        private ClientAPI.ControlMethodDelegate _helloServerSuccessDelegate = null;
        private ClientAPI.ControlMethodDelegate _onlineStatusSucessDelegate = null;
        private ClientAPI.ControlMethodDelegate _controlMethodFailDelegate = null;

        public Page()
        {
            InitializeComponent();
            HtmlPage.RegisterScriptableObject("PageJS", this);  //allow javascript to interact with this object

            //create event handlers to handle async callbacks
            _helloServerSuccessDelegate = new ClientAPI.ControlMethodDelegate(helloServerSuccess);
            _onlineStatusSucessDelegate = new ClientAPI.ControlMethodDelegate(onlineStatusSuccess);
            _controlMethodFailDelegate = new ClientAPI.ControlMethodDelegate(controlMethodCallbackFail);
        }

        private void btnHello_Click(object sender, RoutedEventArgs e)
        {
            AjaxAnimation.Begin();

            ClientAPI.InvokeControlMethod(CallbackID, "Hello", new Dictionary<string, object>()
                {
                    {"Name", UserName},
                    {"Description", "Some Desc"}
                },
            _helloServerSuccessDelegate, _controlMethodFailDelegate);
        }

        private void btnOnlineStatus_Click(object sender, RoutedEventArgs e)
        {
            AjaxAnimation.Begin();
            ClientAPI.InvokeControlMethod(CallbackID, "GetUserInfo", null, _onlineStatusSucessDelegate, _controlMethodFailDelegate);
        }

        private void helloServerSuccess(object payload, object context, object request)
        {
            AjaxAnimation.Stop();
            lblMessage.Text = payload.ToString();
        }

        private void onlineStatusSuccess(object payload, object context, object request)
        {
            AjaxAnimation.Stop();
            var user = payload as ScriptObject;
            var userID = int.Parse(user.GetProperty("UserID").ToString());
            if (userID > 0) //if user logged in
            {
                //obtain logged in user name
                UserName = user.GetProperty("DisplayName").ToString();
                lblMessage.Text = string.Format("Hello {0}, I know who you are!", UserName);
                
                if (RealNameObtained != null)   //if javascript registered event, raise it
                    RealNameObtained(this, new NameObtainedEventArgs(UserName));
            }
            else
                lblMessage.Text = "You are not logged in.  I cannot speak to you!";
        }

        private void controlMethodCallbackFail(object payload, object context, object request)
        {
            AjaxAnimation.Stop();
            MessageBox.Show("ERROR: " + payload.ToString());
        }

    }

}
